# -*- coding: utf-8 -*-

import platform
import os
import playctrl
import ctypes
import HCNetSDK_header

class xyle_ptz_control:
    # 设备信息
    DEV_IP = "61.177.75.106"
    DEV_PORT = 8000
    DEV_USER_NAME = 'admin'
    DEV_PASSWORD = 'abc12345'
    WINDOWS_FLAG = True

    # HCNetSDK库文件路径
    HCNETSDK_DLL_PATH = r'./HCNetSDK.dll'
    HCNETSDK_DLL_PATH_LINUX = r'./libhcnetsdk.so'

    def GetPlatform(self):
        sysstr = platform.system()
        if sysstr != "Windows":
            self.WINDOWS_FLAG = False
        return

    def LoadHCNetSDK(self):
        '''
        加载HCNetSDK库
        '''
        if not self.WINDOWS_FLAG:
            Objdll = ctypes.cdll.LoadLibrary(self.HCNETSDK_DLL_PATH_LINUX)
        else:
            Objdll = ctypes.WinDLL(self.HCNETSDK_DLL_PATH)
        return Objdll

    def InitHCNetSDK(self,Objdll):
        '''
        初始化HCNetSDK库
        '''
        if not self.WINDOWS_FLAG:
            sdk_path = HCNetSDK_header.NET_DVR_LOCAL_SDK_PATH()
            sdk_path.sPath = os.path.dirname(self.HCNETSDK_DLL_PATH).encode('utf-8')
            Objdll.NET_DVR_SetSDKInitCfg(2, ctypes.byref(sdk_path))

        # 初始化DLL
        Objdll.NET_DVR_Init()
        # 设置日志路径
        if not self.WINDOWS_FLAG:
            Objdll.NET_DVR_SetLogToFile(3, r'/home/sdklog', True)
        else:
            Objdll.NET_DVR_SetLogToFile(3, r'C:\\SdkLog', True)
        # 设置设备超时时间
        Objdll.NET_DVR_SetConnectTime(int(2000), 1)
        return

    def LoginDev(self,Objdll):
        '''
        登录设备
        '''
        device_info = HCNetSDK_header.NET_DVR_DEVICEINFO_V30()
        print(self.DEV_IP)
        print(self.DEV_PORT)
        print(self.DEV_USER_NAME)
        print(self.DEV_PASSWORD)

        lUserId = Objdll.NET_DVR_Login_V30(bytes(self.DEV_IP, 'utf-8'), self.DEV_PORT, bytes(self.DEV_USER_NAME, 'utf-8') \
                                           , bytes(self.DEV_PASSWORD, 'utf-8'), ctypes.byref(device_info))

        return (lUserId, device_info)

    def RealDataCallBack_V30(self,Playctrldll, dwDataType, pBuffer, dwBufSize, pUser):
        '''
        码流回调函数
        '''
        pass

    def OpenPreview(self, Objdll, lUserId, callbackFun):
        '''
        打开预览
        '''

        preview_info = HCNetSDK_header.NET_DVR_PREVIEWINFO()
        preview_info.lChannel = 1  # 通道号
        preview_info.dwStreamType = 0  # 主码流
        preview_info.dwLinkMode = 0  # TCP
        preview_info.bBlocked = 1  # 阻塞取流

        lRealPlayHandle = Objdll.NET_DVR_RealPlay_V40(lUserId, ctypes.byref(preview_info) , None, None)
        return lRealPlayHandle